<?php

namespace App\Http\Controllers;

use App\Models\Activity;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ActivityController extends Controller
{
    public function index(Request $request)
    {
        $query = Activity::with('type');
        
        // 搜索条件
        if ($request->has('title')) {
            $query->where('title', 'like', '%' . $request->title . '%');
        }
        if ($request->has('type_id')) {
            $query->where('type_id', $request->type_id);
        }
        if ($request->has('status')) {
            $query->where('status', $request->status);
        }
        if ($request->has('is_recommended')) {
            $query->where('is_recommended', $request->is_recommended);
        }
        if ($request->has('is_online')) {
            $query->where('is_online', $request->is_online);
        }

        $activities = $query->orderBy('create_time', 'desc')->paginate(10);
        
        return response()->json([
            'code' => 200,
            'msg' => 'success',
            'data' => $activities
        ]);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required|string|max:200',
            'type_id' => 'required|integer',
            'start_time' => 'required|date',
            'end_time' => 'required|date|after:start_time',
            'max_people' => 'required|integer|min:0',
            'points' => 'required|integer|min:0',
            'content' => 'required|string',
            'is_recommended' => 'boolean',
            'is_online' => 'boolean'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'code' => 1,
                'msg' => $validator->errors()->first()
            ]);
        }

        $activity = Activity::create($request->all());

        return response()->json([
            'code' => 200,
            'msg' => 'success',
            'data' => $activity
        ]);
    }

    public function show($id)
    {
        $activity = Activity::with('type')->find($id);
        
        if (!$activity) {
            return response()->json([
                'code' => 500,
                'msg' => 'Activity not found'
            ]);
        }

        return response()->json([
            'code' => 200,
            'msg' => 'success',
            'data' => $activity
        ]);
    }

    public function update(Request $request, $id)
    {
        $activity = Activity::find($id);
        
        if (!$activity) {
            return response()->json([
                'code' => 500,
                'msg' => 'Activity not found'
            ]);
        }

        $validator = Validator::make($request->all(), [
            'title' => 'string|max:200',
            'type_id' => 'integer',
            'start_time' => 'date',
            'end_time' => 'date|after:start_time',
            'max_people' => 'integer|min:0',
            'points' => 'integer|min:0',
            'content' => 'string',
            'is_recommended' => 'boolean',
            'is_online' => 'boolean'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'code' => 500,
                'msg' => $validator->errors()->first()
            ]);
        }

        $activity->update($request->all());

        return response()->json([
            'code' => 200,
            'msg' => 'success',
            'data' => $activity
        ]);
    }

    public function destroy($id)
    {
        $activity = Activity::find($id);
        
        if (!$activity) {
            return response()->json([
                'code' => 1,
                'msg' => 'Activity not found'
            ]);
        }

        $activity->delete();

        return response()->json([
            'code' => 200,
            'msg' => 'success'
        ]);
    }
} 